package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.HoneycombArticleQueryDTO;
import com.yuanfeng.base.entity.BaseHoneycombArticleEntity;
import com.yuanfeng.base.mapper.BaseHoneycombArticleMapper;
import com.yuanfeng.base.service.BaseHoneycombArticleService;
import com.yuanfeng.base.vo.*;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.util.Json.JsonUtils;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseHoneycombArticleServiceImpl
 * @Description  图文发现表
 * @Author ty
 * @Date 2023-07-04 16:27:37
 * @Version 1.0
 **/
@Service
public class BaseHoneycombArticleServiceImpl extends ServiceImpl<BaseHoneycombArticleMapper, BaseHoneycombArticleEntity> 
        implements BaseHoneycombArticleService {

    @Override
    public ResponseResult queryFinishArticleList(HoneycombArticleQueryDTO param) {
        Map map = null;
        try {
            map = ParseMapUtils.beanToMap(param);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        IPage<HoneyCombArticleListVO> page = this.baseMapper.selectFinishArticleList(new Query<HoneyCombArticleListVO>().getPage(map), param);
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult queryArticleDetail(HoneycombArticleQueryDTO param) {
        Integer articleId = param.getArticleId();
        ArticleDetailVO result = null;
        String token = param.getToken();
        String userId = RedisUtil.get(YFConfig.YF_USER_TOKEN_USERID + token);
        // 判断用户是否关注
        if (StringUtils.isNotBlank(token) && null != userId) {
            param.setUserId(userId);
            result = this.baseMapper.selectHoneycombArticleDetail(param);
        } else {
            result = this.baseMapper.selectHoneycombArticleDetail2(param);
        }
        if (null == result) {
            return ResponseResult.fail(2, "该条信息不存在，id=" + articleId);
        }

        String extendGoodsList = result.getExtendGoodsList();
        String extendGoodsImageList = result.getExtendGoodsImageList();

        // 查询标签
        List<HoneycombLabelVO> labelList = this.baseMapper.selectArticleLabel(articleId);

        // 查询相似文章
        LikeArticleVO likeArticleParam = new LikeArticleVO();
        likeArticleParam.setArticleId(articleId);
        likeArticleParam.setLabelList(labelList);
        List<AriticleInformatioListVO> likeArticles = this.baseMapper.selectLikeArticle(likeArticleParam);

        result.setLabelList(labelList);
        result.setLikeArticles(likeArticles);
        // 相关商品列表
        if (null != extendGoodsList && !"".equals(extendGoodsList)) {

            List<Integer> goodsList = JsonUtils.StringToList(extendGoodsList);
            if (!CollectionUtils.isEmpty(goodsList)) {
                result.setGoodsList(this.baseMapper.selectAppGuessLike3(goodsList));
            }
        } else {
            result.setGoodsList(new ArrayList<>());
        }
        // 图片集
        if (null != extendGoodsImageList && !"".equals(extendGoodsImageList)) {
            List<String> goodsImageList = JsonUtils.StringToList4(extendGoodsImageList);
            if (!CollectionUtils.isEmpty(goodsImageList)) {
                result.setGoodsImageList(goodsImageList);
            }
            return ResponseResult.success(result);
        }
        result.setGoodsImageList(new ArrayList<>());
        return ResponseResult.success(result);
    }
}
